package com.td.erhome.ui.historycost;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.td.erhome.R;
import com.td.erhome.adapter.CostAreaAdapter;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.CityInfoLitePal;

import org.litepal.crud.DataSupport;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class CostAreaActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.re_title)
    RelativeLayout mReTitle;
    @Bind(R.id.recyview_mj)
    RecyclerView mRecyviewMj;
    @Bind(R.id.recyview_cx)
    RecyclerView mRecyviewCx;
    @Bind(R.id.recyview_zx)
    RecyclerView mRecyviewZx;
    @Bind(R.id.recyview_fl)
    RecyclerView mRecyviewFl;
    @Bind(R.id.recyview_xb)
    RecyclerView mRecyviewXb;

    private List<CityInfoLitePal> mCityInfoLitePals;
    private long mArea_id;
    private String mArea_name;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cost_area;
    }

    @Override
    protected void initView() {
         //主城区
        String[] data = {"金牛区","青羊区","成华区","武侯区","锦江区"};
        mRecyviewMj.setLayoutManager(new GridLayoutManager(this, 3));
        CostAreaAdapter adapter = new CostAreaAdapter(this, data);
        mRecyviewMj.setAdapter(adapter);

        //北部
        String[] data1 = {"彭州市","都江堰市","新都区","青白江区","金堂县"};
        mRecyviewCx.setLayoutManager(new GridLayoutManager(this, 3));
        CostAreaAdapter adapter1 = new CostAreaAdapter(this, data1);
        mRecyviewCx.setAdapter(adapter1);

        //东部
        String[] data2 = {"龙泉驿区","简阳市"};
        mRecyviewZx.setLayoutManager(new GridLayoutManager(this, 3));
        CostAreaAdapter adapter2 = new CostAreaAdapter(this, data2);
        mRecyviewZx.setAdapter(adapter2);

        //南部
        String[] data3 = {"双流县","新津县"};
        mRecyviewFl.setLayoutManager(new GridLayoutManager(this, 3));
        CostAreaAdapter adapter3 = new CostAreaAdapter(this, data3);
        mRecyviewFl.setAdapter(adapter3);
        //西部
        String[] data4 = {"温江区","郫县","崇州市","大邑县","邛崃市","温江区","浦江县"};
        mRecyviewXb.setLayoutManager(new GridLayoutManager(this, 3));
        CostAreaAdapter adapter4 = new CostAreaAdapter(this, data4);
        mRecyviewXb.setAdapter(adapter4);

        adapter.setOnItemClickListener(new CostAreaAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(String sn) {
                Intent intent = new Intent(CostAreaActivity.this, CostListActivity.class);
                mCityInfoLitePals = DataSupport.where("area_name LIKE ?", sn).find(CityInfoLitePal.class);
                long area_id = mCityInfoLitePals.get(0).area_id;
                String area_name = mCityInfoLitePals.get(0).area_name;
                intent.putExtra("name",area_name);
                intent.putExtra("id",area_id);
                startActivity(intent);
            }
        }); adapter1.setOnItemClickListener(new CostAreaAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(String sn) {
                Intent intent = new Intent(CostAreaActivity.this, CostListActivity.class);
                mCityInfoLitePals = DataSupport.where("area_name LIKE ?", sn).find(CityInfoLitePal.class);
                long area_id = mCityInfoLitePals.get(0).area_id;
                String area_name = mCityInfoLitePals.get(0).area_name;
                intent.putExtra("name",area_name);
                intent.putExtra("id",area_id);
                startActivity(intent);
            }
        }); adapter2.setOnItemClickListener(new CostAreaAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(String sn) {

                Intent intent = new Intent(CostAreaActivity.this, CostListActivity.class);
                mCityInfoLitePals = DataSupport.where("area_name LIKE ?", sn).find(CityInfoLitePal.class);
                if (mCityInfoLitePals==null||mCityInfoLitePals.size()==0){
                    if (sn.equals("简阳市")){
                        mArea_id = 510185;
                        mArea_name = "简阳市";
                    }else {
                        return;
                    }
                }else {
                    mArea_id = mCityInfoLitePals.get(0).area_id;
                    mArea_name = mCityInfoLitePals.get(0).area_name;
                }
                intent.putExtra("name",mArea_name);
                intent.putExtra("id",mArea_id);
                startActivity(intent);
            }
        }); adapter3.setOnItemClickListener(new CostAreaAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(String sn) {
                Intent intent = new Intent(CostAreaActivity.this, CostListActivity.class);
                mCityInfoLitePals = DataSupport.where("area_name LIKE ?", sn).find(CityInfoLitePal.class);
                long area_id = mCityInfoLitePals.get(0).area_id;
                String area_name = mCityInfoLitePals.get(0).area_name;
                intent.putExtra("name",area_name);
                intent.putExtra("id",area_id);
                startActivity(intent);
            }
        }); adapter4.setOnItemClickListener(new CostAreaAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(String sn) {
                Intent intent = new Intent(CostAreaActivity.this, CostListActivity.class);
                mCityInfoLitePals = DataSupport.where("area_name LIKE ?", sn).find(CityInfoLitePal.class);
                long area_id = mCityInfoLitePals.get(0).area_id;
                String area_name = mCityInfoLitePals.get(0).area_name;
                intent.putExtra("name",area_name);
                intent.putExtra("id",area_id);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void releaseResource() {

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

}
